import maya.cmds as cmds;
cmds.SelectAll();
cmds.Delete();

def pin_solid( h=10.0, r=4.0, lh=3.0,lt=1.0):

    curve2 = cmds.curve(d=1,ep=[
    (0,0,0),
    (r,0,0),                            
    (r,h-lh,0),
    (r+lt/2.0,h-lh+0.25*lh,0),
    (r+lt/2.0,h-lh+0.50*lh,0),
    (r-lt/2.0,h,0),
    (0,h,0),
    ]);
    curve2 = cmds.ls(sl=True)#work around
    cmds.nurbsToPolygonsPref( pc=300 )
    pin = cmds.revolve(curve2[0],ax=(0,1,0),p=(0,0,0),po=1,s=30,d=1);
    centerCut = cmds.polyCube(w=r*0.5, h=h,sy=1,d=r*2+lt*2);
    cmds.move(0,h/2.0+h/4.0,0);
    
    leftCut = cmds.duplicate(centerCut[0]);
    cmds.select(leftCut[0]);
    cmds.move(1.125*r,0,0,relative=True);
    
    rightCut = cmds.duplicate(centerCut[0]);
    cmds.select(rightCut[0]);
    cmds.move(-1.125*r,0,0,relative=True);
    
    centerCylinder = cmds.polyCylinder(radius=r/2.5,h=h,sx=40);#will be trimmed later
    cmds.move(0,h/2.0+h/4,0);
    
    for s in [leftCut[0],rightCut[0],centerCylinder[0],centerCut[0]]:
        cmds.select(pin[0],s);
        pin = cmds.polyBoolOp(op=2);
    cmds.select(curve2);
    cmds.Delete();
    return pin;


def pinHole(h=10, r=4, lh=3, lt=1, t=0.3, tight=True):
    # h = shaft height
    # r = shaft radius
    # lh = lip height
    # lt = lip thickness
    # t = tolerance
    # tight = set to false if you want a joint that spins easily
    if(tight):
        centerCylinder = cmds.polyCylinder(radius=r,h=h+0.2,sx=40);#will be trimmed later
    else:
        print "not tight"
        centerCylinder = cmds.polyCylinder(radius=r+(t/2.0)+0.25,h=h+0.2,sx=40);#will be trimmed later
    cmds.move(0,(h+0.2)/2.0,0);
    
    curve = cmds.curve(d=1,ep=[
    (0,0,0),
    (r+(t/2)+(lt/2),0,0),                                
    (r,lh/3.0,0),
    (0,lh/3.0,0),
    ]);
    curve = cmds.ls(sl=True)#work around
    
    cmds.nurbsToPolygonsPref( pc=300 );
    # widen the entrance hole to make insertion easier    
    bottomTip = cmds.revolve(curve[0],ax=(0,1,0),p=(0,0,0),po=1,s=30,d=1);
    pinHole = pin_solid(h,r+(t/2.0),lh,lt);
    cmds.select(curve);
    cmds.Delete();
    
    for s in [bottomTip[0],centerCylinder[0]]:
        cmds.select(pinHole[0],s);
        pinHole = cmds.polyBoolOp(op=1,useThresholds=1);
    return pinHole;

# pin_solid()